#include <stdio.h>
#include <stdlib.h>
#include "UF.h"

/**
 * Program 4.7 Equivalence-relations ADT client
 * --------------------------------------------
 * 编译：
 * gcc -o TestUF TestUF.c WeightedUF.c
 * --------------------------------------------
 * 运行：
 * ./TestUF 10
 * 3 4
 * 4 9
 * 8 0
 * 2 3
 * 5 6
 * 2 9
 * 5 9
 * 7 3
 * 4 8
 * 5 6
 * 0 2
 * 6 1
 */
int main(int argc, char **argv){
    if (argc != 2) {
        fprintf(stderr, "Usage: %s N\n", argv[0]);
        return 1;
    }
    int N = atoi(argv[1]);
    int p, q;

    UFinit(N);
    while(scanf("%d %d", &p, &q) == 2){
        if (!UFfind(p, q)) {
            UFunion(p, q);
            printf("%d %d\n", p, q);
        }
    }

}